//
//  WJSignatureHistoryCell.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/26.
//

import UIKit

class WJSignatureHistoryCell: UICollectionViewCell {
    
    private lazy var imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.backgroundColor = UIColor(hex: "FFFFFF")
        iv.layer.borderWidth = 1
        iv.layer.borderColor = UIColor(hex: "#121212", alpha: 0.05).cgColor
        iv.layer.cornerRadius = 8
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.font = UIFont.montserratMedium(16)
        label.textColor = UIColor(hex: "#121212", alpha: 0.6)
        label.numberOfLines = 2
        return label
    }()
    
    // 选中状态下的边框视图
    private lazy var selectionBorderView: UIView = {
        let view = UIView()
        view.layer.borderWidth = 2
        view.layer.borderColor = UIColor(hex: "#1F87F6").cgColor
        view.layer.cornerRadius = 12
        view.isHidden = true
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        contentView.addSubview(selectionBorderView)
        contentView.addSubview(imageView)
        contentView.addSubview(nameLabel)
        
        contentView.backgroundColor = UIColor(hex: "#FFFFFF")
        contentView.layer.cornerRadius = 12
        
        // 阴影效果
        contentView.layer.shadowColor = UIColor(hex: "#9B9B9B").cgColor
        contentView.layer.shadowOffset = CGSize(width: 0, height: 4)
        contentView.layer.shadowOpacity = 0.2
        contentView.layer.shadowRadius = 6
        contentView.layer.masksToBounds = false
    }
    
    private func setupConstraints() {
        // 选中边框视图约束
        selectionBorderView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        // 签名图片约束
        imageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.leading.trailing.equalToSuperview().inset(8)
            make.height.equalTo(imageView.snp.width) // 正方形比例
        }
        
        // 名称标签约束
        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(3)
            make.leading.trailing.equalToSuperview().inset(5)
            make.bottom.equalToSuperview().offset(-3)
        }
    }
    
    private func updateSelectionState(isSelected: Bool) {
        selectionBorderView.isHidden = !isSelected
    }
    
    func configure(with model: WJSignatureHistoryModel) {
        imageView.image = model.imageData.flatMap { UIImage(data: $0) }
        nameLabel.text = model.name
        updateSelectionState(isSelected: model.isSelected)
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        imageView.image = nil
        nameLabel.text = nil
    }
}


